<?php

namespace App\Http\Middleware;

use Closure;
use App\Models\User;

class Login
{
    /**
     * Handle an incoming request.
     *
     * @param \Illuminate\Http\Request $request
     * @param \Closure                 $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $uid = $request->session()->get('user_id');
        if (!$uid) {
            return redirect('/login')->with(['code'=>2,'msg'=>'请先登录']);            
        }
        //如果没找到，返回404
        $user = User::findOrFail($uid);
        $request->user = $user;

        return $next($request);
    }
}
